Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose a replace visit on redirection #328

Merged
merged 4 commits into from
Aug 24, 2021

Conversation

jaysson
Copy link
Contributor

@jaysson jaysson commented Jul 30, 2021

  • Updated the browser adapter to remove the call to followRedirect as it now happens in the visit itself
  • Updated the tests to reflect the new behaviour

This should also provide a solution to these issues on native adapters:

In addition, it allows path properties to be applied correctly to redirection destinations in native adapters.

…nd location

* Updated the browser adapter to remove the call to `followRedirect` as it now happens in the visit itself
* Updated the tests to reflect the new behaviour
@jaysson jaysson marked this pull request as ready for review July 30, 2021 08:46
@dhh dhh requested a review from jayohms August 4, 2021 11:23
Copy link
Collaborator

@jayohms jayohms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this @jaysson 👍, been meaning to get this fixed for awhile!

@@ -24,7 +24,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {

this.assert.notOk(await this.formSubmitted)
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
this.assert.equal(await this.visitAction, "advance")
this.assert.equal(await this.visitAction, "replace")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is replace the expected action for GET form submissions?

It looks like we'll want a new test to validate normal visit redirects result in a new visit proposal with a replace action.

Copy link
Contributor Author

@jaysson jaysson Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sequence:

  • Visit the original URL with advance action
  • If there is a redirection, propose replace visit to the destination with the existing response. That way it doesn't fire one more fetch call, instead reuses the existing response.

User will see the same behaviour as before. Just that programmatically the sequence looks like advance + replace instead of just advance.

As long as there is a redirect, replace action is expected at the end. In this test, form is submitted to /__turbo/redirect (ref: https://github.com/jaysson/turbo/blob/replace-on-redirect/src/tests/fixtures/form.html#L41), and that endpoint returns a 303 to /src/tests/fixtures/one.html resulting in replace action.

It looks like we'll want a new test to validate normal visit redirects result in a new visit proposal with a replace action.

Yes, will do.

@jaysson
Copy link
Contributor Author

jaysson commented Aug 8, 2021

@jayohms Added two tests. One verifies that the replace visit is happening, and another verifies that it's not replacing the current page, but pushes a new one in the navigation stack.

@jaysson jaysson requested a review from jayohms August 8, 2021 08:41
@jayohms
Copy link
Collaborator

jayohms commented Aug 19, 2021

Thanks @jaysson, I'll take a closer look at this before the end of the week and test in the native apps.

@dhh dhh added this to the 7.0.0 milestone Aug 19, 2021
Copy link
Collaborator

@jayohms jayohms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaysson This works great on the native side, thanks!

Can you please resolve the merge conflict and let's make sure that the tests pass this time around.

@dhh dhh merged commit 38b8f13 into hotwired:main Aug 24, 2021
@dhh
Copy link
Member

dhh commented Aug 24, 2021

Fixed the merge and tests passed 👍

@domchristie
Copy link
Contributor

@jayohms @jaysson might there be an alternative way to achieve this without proposing another Visit?

I work on an animation library that animates on turbo:visit, assuming it is a user-initiated action. With this change, clicking a link that redirects will trigger the animation twice, and there's no easy way to determine which is the user initiated visit and which is the one triggered by followRedirect

Related: #877

@jayohms
Copy link
Collaborator

jayohms commented Oct 25, 2023

@domchristie The main issue I see is that we map url paths to view controllers/fragments in the path configuration files. The path properties that each path maps to in the path config files determine how/where to navigate. The turbo native libraries propose visits and load the view controller/fragment before the visit actually starts.

So:

  • A visit proposal is initiated
  • The native libraries look up the path config properties to determine what view controller/fragment to load
  • The view controller/fragment is presented
  • The proposed visit is started
  • The redirect is detected and a new "replace" visit is proposed
  • The native libraries look up the path config properties to determine what view controller/fragment to load
  • The view controller/fragment is presented
  • The redirect "replace" visit is started

Since the original url can have different path properties than the redirect url, the double-visit is the best way to work around these issues. Maybe passing along metadata with the turbo:visit event could be another path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants